package com.demo;

/*
wait notify
 */

import java.util.ArrayList;
import java.util.List;

class Demo7Thread {
    private List<String> list = new ArrayList<>();

    public synchronized void add(String item) {
        list.add(item);
        this.notifyAll();
    }

    public synchronized String get() throws InterruptedException {
        while (this.list.isEmpty()) {
            System.out.println("wait");
            this.wait();
        }
        return list.remove(0);
    }
}
public class Demo7 {

    public static void main(String[] args) throws InterruptedException {
        Demo7Thread demo7Thread = new Demo7Thread();
        for (int j = 0; j < 6; j++) {
            new Thread(() -> {
                try {
                    System.out.println(demo7Thread.get());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }

        Thread.sleep(3000);
        for (int i = 0; i < 3; i++) {
            String name = "item:" + i;
            new Thread(() -> {
                demo7Thread.add(name);
            }).start();
        }

    }
}
